import {HttpHeaders, HttpParamsOptions} from "@angular/common/http";
import {InterceptorHttpParams} from "@core/interceptor/InterceptorHttpParams";
import {InterceptorConfig} from "@core/interceptor/InterceptorConfig";

export interface RequestConfig {
  ignoreLoading?: boolean;
  ignoreErrors?: boolean;
  resendRequest?: boolean;
}

class HttpUtil {
  defaultHttpOption(ignoreLoading: boolean = false,
                    ignoreErrors: boolean = false,
                    resendRequest: boolean = false): HttpOptions {
    return {
      headers: new HttpHeaders({
        'Content-Type': 'application/json; charset=UTF-8',
      }),
      params: new InterceptorHttpParams(new InterceptorConfig(ignoreLoading, ignoreErrors, resendRequest))
    }
  }

  defaultHttpOptionFromConfig(config: RequestConfig | undefined): HttpOptions {
    if (config !== undefined) {
      return this.defaultHttpOption(config.ignoreLoading, config.ignoreErrors, config.resendRequest)
    }
    return {} as HttpOptions;
  }
}

const httpUtil = new HttpUtil();
export default httpUtil;
